﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using com.codeplex.eatoutadvocate.BL;
using System.Configuration;

namespace com.codeplex.EatOutAdvocate.Web
{
    public partial class SiteMaster : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string cookieName = "ZipCode";

            if (HttpContext.Current.Request.Cookies[cookieName] == null)
            {
                HttpRequest request = base.Request; // Get request
                string ip = request.UserHostAddress; // Get User host address

                /*
                string ip = Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; // Forwarded IP
                if (ip == null)
                {
                    ip = Request.ServerVariables["REMOTE_ADDR"].ToString();
                }
                 * */

                string LocationAppKey = ConfigurationManager.AppSettings["LocationAppKey"].ToString();

                string zipCode = Location.getZipCode(ip, LocationAppKey);
                HttpCookie cookie = new HttpCookie(cookieName, zipCode);
                cookie.Expires.AddDays(7.0);
                Response.Cookies.Add(cookie);
            }
            
        }        
            
    }
}


